﻿Imports DevExpress
Imports DevExpress.XtraTreeList
Imports DevExpress.XtraTreeList.Nodes

Public Class UcnComboboxTreelist
    'represents PopupContainerControl for combobox implemented with treelist

#Region "Attributes"
#Region "Private attributes"
    Private MoSelectedRow As TreeListNode = Nothing 'actually selected row
#End Region
#End Region
#Region "Properties"
#Region "Public properties"
    'result of this "control"
    Public ReadOnly Property Result() As Integer
        Get
            If MoSelectedRow IsNot Nothing Then
                Return MoSelectedRow.GetValue("PropertyID")
            Else
                Return -1
            End If
        End Get
    End Property
#End Region
#End Region
#Region "Constructors"
    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Me.tlTreeList.Nodes.Clear()
        MoSelectedRow = Me.tlTreeList.FocusedNode

    End Sub
#End Region
#Region "Events handler"
    'drawing a cell
    Private Sub tlTreeList_CustomDrawNodeCell(ByVal sender As Object, ByVal e As DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs) Handles tlTreeList.CustomDrawNodeCell
        If e.Node.Selected Then
            e.Appearance.BackColor = Me.tlTreeList.Appearance.FocusedCell.BackColor
            e.Appearance.ForeColor = Color.White
        End If
    End Sub
    'keypressed - confirmation of result
    Private Sub tlTreeList_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tlTreeList.KeyPress
        If Asc(e.KeyChar) <> Keys.Escape Then 'ESC
            MoSelectedRow = Me.tlTreeList.FocusedNode
            PopupContainerControl.OwnerEdit.ClosePopup()
        End If
    End Sub
    'mouseclick - confirmation of result
    Private Sub tlTreeList_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tlTreeList.MouseClick
        Dim LoNode As TreeListNode = Me.tlTreeList.CalcHitInfo(New Point(e.X, e.Y)).Node
        If (e.Button = Windows.Forms.MouseButtons.Left) And (LoNode IsNot Nothing) Then
            MoSelectedRow = LoNode
            PopupContainerControl.OwnerEdit.ClosePopup()
        End If
    End Sub
    'mouse move - focusing node under mouse pointer
    Private Sub tlTreeList_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tlTreeList.MouseMove
        Dim LoHitInfo As TreeListHitInfo = Me.tlTreeList.CalcHitInfo(New Point(e.X, e.Y))
        If LoHitInfo.Node IsNot Nothing Then
            Me.tlTreeList.FocusedNode = LoHitInfo.Node
        End If
    End Sub
#End Region
#Region "Subs"
    'initialization of controls
    Public Sub Init(ByVal IsCaptionText As String, ByVal IoTextCollection As Collection, Optional ByVal IiSelectedRow As Integer = 0, Optional ByVal IoIdCollection As Collection = Nothing)
        Dim LbIsIdList As Boolean = False
        Dim LoIdCollection As Collection = New Collection

        Me.tlTreeList.ClearSorting()
        Me.tlTreeList.Columns("PropertyValue").Caption = IsCaptionText

        If IoIdCollection IsNot Nothing Then
            If IoTextCollection.Count <> IoIdCollection.Count Then
                Debug.Print("CHYBA!!! - UcnComboboxTreelist.Init")
                LbIsIdList = False
            Else
                LbIsIdList = True
            End If
        Else
            LbIsIdList = False
        End If

        'creation of new list with ID for values
        If Not LbIsIdList Then
            LoIdCollection.Clear()
            For LiI As Integer = 0 To IoTextCollection.Count - 1
                LoIdCollection.Add(LiI)
            Next
        Else
            LoIdCollection = IoIdCollection
        End If

        Dim LoNode As TreeListNode = Nothing
        Dim LoSelectedNode As TreeListNode = Nothing
        Me.tlTreeList.Nodes.Clear()
        For LiI As Integer = 0 To IoTextCollection.Count - 1
            LoNode = Me.tlTreeList.AppendNode(New Object() {IoTextCollection(LiI + 1).ToString, LoIdCollection(LiI + 1), LiI}, -1)
            If LiI = IiSelectedRow Then
                LoSelectedNode = LoNode
            End If
        Next

        'selecting row
        Me.tlTreeList.FocusedNode = LoSelectedNode
    End Sub
#End Region

End Class